package com.xudow.app.dynamicstate_old.module.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.domain.entity.AgencyTeamMember;
import com.xudow.app.dynamicstate_old.domain.entity.UserCheckImage;
import com.xudow.app.dynamicstate_old.domain.entity.UserIntroduce;
import com.xudow.app.dynamicstate_old.domain.entity.UserIntroduceImages;
import com.xudow.app.dynamicstate_old.module.PictureActivity;
import com.xudow.app.dynamicstate_old.widget.exview.NetImageAdapter;
import com.xudow.app.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ske on 2016/6/14.
 */
public class IntroduceFragment extends Fragment {
    Unbinder unbinder;
    @BindView(R.id.photos)
    RecyclerView recycler;
    @BindView(R.id.text_work_time)
    TextView textWorkTime;
    @BindView(R.id.text_introduce)
    TextView textIntroduce;
    @BindView(R.id.text_achievement)
    TextView textAchievement;
    @BindView(R.id.text_experience)
    TextView textExperience;
    @BindView(R.id.content)
    View content;
    @BindView(R.id.empty)
    View empty;
    @BindView(R.id.btn_write)
    View write;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.title_work)
    TextView titleWork;
    @BindView(R.id.title_introduce)
    TextView titleIntroduce;
    @BindView(R.id.title_achievement)
    TextView titleAchievement;
    @BindView(R.id.title_experience)
    TextView titleExperience;
    @BindView(R.id.ll_team)
    LinearLayout llTeam;

    public static IntroduceFragment newInstance(UserIntroduce introduce, String data) {
        Bundle args = new Bundle();
        args.putSerializable("user", introduce);
        args.putString("data", data);
        IntroduceFragment fragment = new IntroduceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    UserIntroduce introduce;
    ImageAdapter imageAdapter;
    ArrayList<String> imgPaths;
    String data;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        introduce = (UserIntroduce) getArguments().getSerializable("user");
        data = getArguments().getString("data");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_introduce, container, false);
        unbinder = ButterKnife.bind(this, view);
        setupView();
        return view;
    }

    private void setupView() {
        if (!StringUtils.isEmpty(introduce.getIntroduce())) {
            if (introduce.getUsertype() == 1) {
                titleWork.setText("成立时间");
                titleIntroduce.setText("机构简介");
                titleAchievement.setText("机构成果");
                titleExperience.setText("机构历程");
                List<AgencyTeamMember> members = introduce.getCoreTeamList();
                if (members != null && members.size() > 0) {
                    llTeam.setVisibility(View.VISIBLE);
                    LayoutInflater inflater = LayoutInflater.from(getActivity());
                    for (AgencyTeamMember member : members) {
                        View view = inflater.inflate(R.layout.item_agency_team_member, null);
                        Glide.with(this).load(member.getImagePath()).into((CircleImageView) view.findViewById(R.id.photo));
                        ((TextView) view.findViewById(R.id.text_des)).setText(member.getIntroduce());
                        llTeam.addView(view);
                    }
                }
            }
            textWorkTime.setText(introduce.getWorkTime() + "年");
            textIntroduce.setText(introduce.getIntroduce());
            textAchievement.setText(introduce.getAchievement());
            textExperience.setText(introduce.getExperience());
            recycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
            recycler.setAdapter(imageAdapter = new ImageAdapter(getActivity()));
            imgPaths = new ArrayList<>();
            List<UserCheckImage> checkImages = introduce.getImgCheckList();
            for (UserCheckImage image : checkImages) {
                imgPaths.add(image.getImgPath());
            }
            List<UserIntroduceImages> images = introduce.getImgList();
            for (UserIntroduceImages image : images) {
                imgPaths.add(image.getImgPath());
            }
            imageAdapter.clear();
//            recycler.setColumnCount(5);
            imageAdapter.addAll(imgPaths);
            imageAdapter.notifyDataSetChanged();
            imageAdapter.setOnItemClickListener(position -> {
                Intent i = new Intent(getActivity(), PictureActivity.class);
                i.putExtra(PictureActivity.KEY_INDEX, position);
                i.putExtra(PictureActivity.KEY_PICTURES, imgPaths);
                startActivity(i);
            });
        } else {
            empty.setVisibility(View.VISIBLE);
            content.setVisibility(View.GONE);
            if (introduce.getUserProfileId() != XApplication.getInstance().getUserProfileId()) {
                write.setVisibility(View.GONE);
                text.setText("亲,还没有简介哟!");
            }
            write.setOnClickListener(v -> {
                Intent intent = new Intent(getActivity(), WriteIntroduceActivity.class);
                intent.putExtra("data", data);
                startActivity(intent);
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    class ImageAdapter extends RecyclerArrayAdapter<String> {

        public ImageAdapter(Context context) {
            super(context);
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new ImageViewHolder(parent);
        }
    }

    class ImageViewHolder extends BaseViewHolder<String> {

        @BindView(R.id.image)
        ImageView image;

        public ImageViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_image);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(String data) {
            Glide.with(getActivity()).load(ImageModel.getMiddleImage(data)).into(image);
        }
    }
}
